#include "thread_pool.h"
#include <mysql/field_types.h>
#include <mysql/mysql.h>

extern MYSQL* conn;
void regCommand1(task_t * task){
    //消息
    Msg m;

    char * stmt_str = "SELECT userid FROM t_user WHERE username=?";

    MYSQL_STMT * stmt = mysql_stmt_init(conn);
    if(!stmt){
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
        return;
    }

    int ret = mysql_stmt_prepare(stmt, stmt_str, strlen(stmt_str));
    if(ret){
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
    }
    int param_cnt = mysql_stmt_param_count(stmt);
    if(param_cnt != 1){
        fprintf(stderr, "(%d,%s)\n", mysql_stmt_errno(stmt), 
                mysql_stmt_error(stmt));
        return ;
    }

    char username[60] = {0};
    unsigned long username_length = 0;
    MYSQL_BIND param_binds = {0};
    bind_input(&param_binds, MYSQL_TYPE_VAR_STRING, username, 
             &username_length, 0);

    strcpy(username, task->data);
    printf("username:%s\n", username);
    username_length = strlen(username);
    ret = mysql_stmt_bind_param(stmt, &param_binds);

    ret = mysql_stmt_execute(stmt);

    ret = mysql_stmt_store_result(stmt);

    int count = mysql_stmt_num_rows(stmt);
    printf("username count is %d\n", count);
    if(count == 0){
        //说明没有重复的用户名,生成12个字符的盐值发回去
        char salt_buf[13] = {0};
        getsalt(salt_buf);
        printf("salt_buf: %s\n", salt_buf);
        memset(&m, 0, sizeof(m));
        strcpy(m.data, salt_buf);
        m.type = MSG_TYPE_SUCCESS;
        m.len = strlen(m.data);
        sendn(task->peerfd, &m, 4 + 4 + m.len);
    } else {
        memset(&m, 0, sizeof(m));
        m.type = MSG_TYPE_DUPLICATE;
        m.len = strlen(m.data);
        sendn(task->peerfd, &m, 4 + 4 + m.len);
    }

    mysql_stmt_free_result(stmt);
}

void regCommand2(task_t * task){
    Msg m;

    char* token = strtok(task->data,":");
    char username[64] = {0};
    strcpy(username, token);
    unsigned long username_length = strlen(username);
    token = strtok(NULL, ":");
    char salt[13] = {0};
    strcpy(salt, token);
    unsigned long salt_length = strlen(salt);
    token = strtok(NULL, ":");
    char passwd[99] = {0};
    strcpy(passwd, token);
    unsigned long passwd_length = strlen(passwd);

    printf("username: %s\n", username);
    printf("salt: %s\n", salt);
    printf("passwd: %s\n", passwd);


    char* stmt_str = "INSERT INTO t_user(username, salt, password, pwd, pwdid) VALUES(?,?,?,'/',0)";
    MYSQL_STMT* stmt = mysql_stmt_init(conn);
    if(!stmt){
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
    }
    int ret = mysql_stmt_prepare(stmt, stmt_str, strlen(stmt_str));
    if(ret){
        fprintf(stderr, "(%d,%s)\n", 
                mysql_stmt_errno(stmt), mysql_stmt_error(stmt));

    }
    int param_cnt = mysql_stmt_param_count(stmt);
    printf("param count: %d\n", param_cnt);

    MYSQL_BIND param_binds[3];
    bind_input(&param_binds[0], 
               MYSQL_TYPE_VAR_STRING, 
               username, 
               &username_length, 0);
    bind_input(&param_binds[1], 
               MYSQL_TYPE_VAR_STRING, 
               salt, 
               &salt_length, 0);
    bind_input(&param_binds[2], 
               MYSQL_TYPE_VAR_STRING, 
               passwd, 
               &passwd_length, 0);
    mysql_stmt_bind_param(stmt, param_binds);

    mysql_stmt_execute(stmt);

    int row = mysql_stmt_affected_rows(stmt);

    if(row == 1){   //成功
        memset(&m, 0, sizeof(m));
        m.type = MSG_TYPE_SUCCESS;
        m.len = strlen(m.data);
        sendn(task->peerfd, &m, 4 + 4 + m.len);
    } else {    //失败
        memset(&m, 0, sizeof(m));
        m.type = MSG_TYPE_FAILURE;
        m.len = strlen(m.data);
        sendn(task->peerfd, &m, 4 + 4 + m.len);
    }

    mysql_stmt_free_result(stmt);

}

void regCommand(task_t* task){
    //初始化结构体MYSQL
    MYSQL *conn = mysql_init(NULL);
    const char* host = "localhost";
    const char* user = "root";
    const char* passwd = "1234";
    const char* db = "network_disk";
    //与MySQL服务器建立连接
    conn = mysql_real_connect(conn, host, user, passwd, db, 0, NULL, 0);
    if(!conn){
        fprintf(stderr, "%s.\n", mysql_error(conn));
        return ;
    }
    //执行查询操作
    const char * query = "SELECT salt FROM t_user WHERE username=11";
    int ret = mysql_query(conn, query);
    if(ret != 0){
        fprintf(stderr, "(%d,%s)\n",
                mysql_errno(conn),
                mysql_error(conn));
        return ;
    }
    //获取查询的结果集
    MYSQL_RES *res = mysql_store_result(conn);

    // 获取结果集的行数
    int num_rows = mysql_num_rows(res);
    printf("Number of rows: %d\n", num_rows);
    mysql_close(conn);
}
